

import UIKit
extension UIBarButtonItem
{
    //便利构造函数，在扩展中只能添加便利构造函数
    convenience init(imageName:String,target:AnyObject?,action:Selector)
    {
        //创建按钮
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        //设置点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        //创建UIBarButtonItem，需要调用自身的指定的构造函数
        self.init(customView:button)
    }
}